package lelouet.datacenter.simulation.vms;

import lelouet.datacenter.simulation.PhysicalRequirements;

public class PhysicalRequirementsImpl implements PhysicalRequirements {

	public PhysicalRequirementsImpl(long maxCPU, long memreq, long memuse) {
		this.maxCPU = maxCPU;
		memRequired = memreq;
		memUsed = memuse;
	}

	public PhysicalRequirementsImpl() {
		this(0, 0, 0);
	}

	public PhysicalRequirementsImpl(PhysicalRequirements toSnap) {
		this(toSnap.getMaxCPU(), toSnap.getMemNeeds(), toSnap.getMemUsed());
	}

	protected long maxCPU;

	@Override
	public long getMaxCPU() {
		return maxCPU;
	}

	protected void setMaxCPU(long MIPS) {
		maxCPU = MIPS;
	}

	protected long memUsed;

	@Override
	public long getMemUsed() {
		return memUsed;
	}

	protected void setMemUsed(long size) {
		memUsed = size;
	}

	protected long memRequired;

	@Override
	public long getMemNeeds() {
		return memRequired;
	}

	protected void setMemNeeds(long needs) {
		memRequired = needs;
	}

	@Override
	public String toString() {
		return showSpecs(this);
	}

	public static String showSpecs(PhysicalRequirements reqs) {
		return "(" + smalllong(reqs.getMaxCPU()) + " maxMIPS)("
				+ smalllong(reqs.getMemUsed()) + " MoUsed)("
				+ smalllong(reqs.getMemNeeds()) + " MoReq)";
	}

	private static final int maxlength = 8;
	private static final long upperBound = (long) Math.pow(10.0, maxlength);

	public static String smalllong(long l) {
		if (l >= Long.MAX_VALUE / 10) {
			return "+inf";
		}
		if (l >= upperBound) {
			int log = (int) Math.floor(Math.log10(l));
			double mantissa = l / Math.pow(10.0, log);
			// System.out.println("value : " + l + " mantissa : " + mantissa
			// + " , log : " + log);
			String mantissas = Double.toString(mantissa);
			int mantissaMaxSize = maxlength - 1 - (log < 10 ? 1 : 2);
			if (mantissas.length() > mantissaMaxSize) {
				mantissas = mantissas.substring(0, mantissaMaxSize);
			}
			return mantissas + "e" + log;
		}
		return Long.toString(l);
	}

}
